<?php
/**
 * @file name : <CTR_NAME>.php
 * @package   : <PROJ_NAME>
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : <CPR>
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class <UCF_CTR_NAME> extends Base_Controller
{
	/**
	 * __construct
	 * @access public
	 * @param
	 * @return
	 */
	public function __construct()
	{
		parent::__construct();
	}
	/**
	  * _remap
	  * @access public
	  * @param
	  * @return
	  */
	public function _remap( $method )
	{
		switch ($method) {

			case 'save':
				$this->save<UCF_CTR_NAME>();
				break;

			default:
				$this->index();
		}
	}
	/**
	 * @function index
	 * @access	 private
	 * @param
	 * @return
	 */
	private function index()
	{
	    $controlName    = $this->input->get_post('c');
        /**
         * Get message when save insert or edit submit
         *
         */
        $msgBoxDisp = '';
        $clsType    = '';
        $clsIcon    = '';

        $messages = $this->getErrMsg($msgBoxDisp, $clsType, $clsIcon);

        $arrWhere = array();
		$arrList = $this-><CTR_NAME>_model->loadList($arrWhere);

        $objList = $this->lang_model->getLang();
        $id      = '';
		foreach($objList as $k=>$v) {
			$arrLang[$k]['id'] = $v->id;
			$arrLang[$k]['name'] = $v->name;
			foreach ($arrList as $item) {
			    if ($item->lang_id == $v->id) {
                    $arrLang[$k]['containt'] = $item-><PRI_TBL_NAME>;
                    $id = $item-><PRI_TBL_ID>;
			    }
			}
		}
        $data = array('urlControlName'  => '?c=' . $controlName,
                      'actionForm'		=> '?c=' . $controlName . '&m=save',
                      'langList'		=> $arrLang,
                      '<PRI_TBL_ID>'			=> $id,
        			  'messages'        => $messages,
                      'clsType'         => $clsType,
                      'clsIcon'         => $clsIcon,
                      'msgBoxDisp'      => $msgBoxDisp);

		$this->msmarty->view($controlName . '/admin.<CTR_NAME>_addedit', $data);
	}
	/**
	 * @function save<UCF_CTR_NAME>
	 *
	 * @author	 Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access	 private
	 * @param
	 * @return
	 */
	private function save<UCF_CTR_NAME>()
	{
        $controlName    = $this->input->get_post('c');
        if ($_SERVER['REQUEST_METHOD']==='POST') {
            $primary_id = '<PRI_TBL_ID>';
            $primary_name = '<PRI_TBL_NAME>';

            $data[$primary_name] = $this->input->post('CONTAINT');
            $pri_id     = $this->input->post($primary_id);

            if (is_numeric($pri_id) && $pri_id > 0) {
                $fUpdate = true;
                $data[$primary_id] = $pri_id;
            } else {
                $fUpdate = false;
            }
        	/**
			 * Load lang to show msg
			 */
			$this->lang->load('success', $this->lang_tag);
			$this->lang->load('error', $this->lang_tag);

			///// Start transaction /////
			/////////////////////////////
			$this->db->trans_begin();

			$ret = $this-><CTR_NAME>_model->store($data, $primary_id, true, $fUpdate);

			if ($ret == FALSE) {
			    $this->messages->add($this->lang->line('db_insert_update_err'), 'error');
			    // Rollback transaction
			    $this->db->trans_rollback();
			} else {
                if ($fUpdate) {
                    $this->messages->add($this->lang->line('db_update_success'), 'success');
                } else {
                    $this->messages->add($this->lang->line('db_insert_success'), 'success');
                }
                // Commit transaction
                $this->db->trans_commit();
			}
        }
        defaultPage($controlName);
	}
    /**
     * @function getErrMsg
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   private
     * @param    string $msgBoxDisp, $clsType, $clsIcon
     * @return   array
     */
    private function getErrMsg(&$msgBoxDisp, &$clsType, &$clsIcon)
    {
        $messages = $this->messages->get();

        if (!is_array($messages)) {
            $messages = array();
            $msgBoxDisp = 'none;';
        } else {
            foreach($messages as $type => $message) {
                if(!empty($message)) {
                    if ($type == 'error') {
                        $clsType = $type;
                        $clsIcon = 'errorIcon';
                    } else if ($type == 'success') {
                        $clsType = $type;
                        $clsIcon = 'successIcon';
                    }
                    break;
                }
            }
        }
        $this->messages->clear();
        return $messages;
    }
}